package com.ndsoft.service.core.interceptors;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class TimeInterceptor implements HandlerInterceptor {

	private static final Log log = LogFactory.getLog(TimeInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		log.debug("========preHandle=========");
//		log.debug(((HandlerMethod) handler).getBean().getClass().getName());
//		log.debug(((HandlerMethod) handler).getMethod().getName());

		log.debug("------Request information ------");
		log.debug("[request.getContextPath()]:" + request.getContextPath());
		log.debug("[request.getScheme()]:" + request.getScheme());
		log.debug("[request.getProtocol()]:" + request.getProtocol());
		log.debug("[request.getServerName()]:" + request.getServerName());
		log.debug("[request.getServerPort()]:" + request.getServerPort());
		log.debug("[request.getLocalAddr()]:" + request.getLocalAddr());
		log.debug("[request.getLocalName()]:" + request.getLocalName());
		log.debug("[request.getLocalPort():]" + request.getLocalPort());
		log.debug("[request.getMethod():]" + request.getMethod());
		log.debug("[request.getRequestURI():]" + request.getRequestURI());
		log.debug("[request.getRequestURL():]" + request.getRequestURL());
		log.debug("[request.getServletPath():]" + request.getServletPath());
		log.debug("[request.getServletContext().getRealPath('/'):]" + request.getServletContext().getRealPath("/"));
		log.debug("[request.getSession().getServletContext().getRealPath('/'):]"
				+ request.getSession().getServletContext().getRealPath("/"));
		log.debug("------Client information------");
		log.debug("[request.getRemoteAddr():]" + request.getRemoteAddr());
		log.debug("[request.getRemoteHost():]" + request.getRemoteHost());
		log.debug("[request.getRemotePort():]" + request.getRemotePort());
		Map map = new HashMap();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName, paramValue);
				}
			}
		}
		Set<Map.Entry<String, String>> set = map.entrySet();
		log.debug("------Request Params inforamtion------------------------");
		for (Map.Entry entry : set) {
			log.debug(entry.getKey() + ":" + entry.getValue());
		}
		log.debug("------Request Params inforamtion end--------------------");
		request.setAttribute("startTime", System.currentTimeMillis());
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		log.debug("========postHandle=========");
		Long start = (Long) request.getAttribute("startTime");
		log.debug("耗时:" + (System.currentTimeMillis() - start));
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception exception) throws Exception {

		log.debug("========afterCompletion=========");
		Long start = (Long) request.getAttribute("startTime");
		log.debug("耗时:" + (System.currentTimeMillis() - start));
		log.debug(exception);
	}

}
